package com.dianping.cat.consumer.state.model.transform;

import com.dianping.cat.consumer.state.model.IEntity;
import com.dianping.cat.consumer.state.model.entity.Detail;
import com.dianping.cat.consumer.state.model.entity.Machine;
import com.dianping.cat.consumer.state.model.entity.Message;
import com.dianping.cat.consumer.state.model.entity.ProcessDomain;
import com.dianping.cat.consumer.state.model.entity.StateReport;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;









public class DefaultSaxParser
  extends DefaultHandler
{
/*  38 */   private DefaultLinker m_linker = new DefaultLinker(true);

/*  40 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  42 */   private Stack<String> m_tags = new Stack();

/*  44 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  48 */   private StringBuilder m_text = new StringBuilder();


/*  51 */   public static StateReport parse(InputStream in) throws SAXException, IOException { return (StateReport)parseEntity(StateReport.class, new InputSource((InputStream)removeBOM(in))); }



/*  55 */   public static StateReport parse(Reader reader) throws SAXException, IOException { return (StateReport)parseEntity(StateReport.class, new InputSource((Reader)removeBOM(reader))); }



/*  59 */   public static StateReport parse(String xml) throws SAXException, IOException { return (StateReport)parseEntity(StateReport.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  65 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  66 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  68 */       factory.setValidating(false);
/*  69 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  71 */       factory.newSAXParser().parse(is, handler);
/*  72 */       return (T)handler.getEntity();
/*  73 */     } catch (ParserConfigurationException e) {
/*  74 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  79 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  83 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  89 */     if (obj instanceof String) {
/*  90 */       String str = (String)obj;

/*  92 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  93 */         return (T)str.substring(1);
      }
/*  95 */       return obj;
    }
/*  97 */     if (obj instanceof InputStream) {
/*  98 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/* 100 */       in.mark(3);

/* 102 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 103 */         in.reset();
      }

/* 106 */       return (T)in;
/* 107 */     }  if (obj instanceof Reader) {
/* 108 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 110 */       in.mark(1);

/* 112 */       if (in.read() != 65279) {
/* 113 */         in.reset();
      }

/* 116 */       return (T)in;
    }
/* 118 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 124 */     if (value == null || value.length() == 0) {
/* 125 */       return defaultValue;
    }

/* 128 */     if (type == Boolean.class)
/* 129 */       return (T)Boolean.valueOf(value); 
/* 130 */     if (type == Integer.class)
/* 131 */       return (T)Integer.valueOf(value); 
/* 132 */     if (type == Long.class)
/* 133 */       return (T)Long.valueOf(value); 
/* 134 */     if (type == Short.class)
/* 135 */       return (T)Short.valueOf(value); 
/* 136 */     if (type == Float.class)
/* 137 */       return (T)Float.valueOf(value); 
/* 138 */     if (type == Double.class)
/* 139 */       return (T)Double.valueOf(value); 
/* 140 */     if (type == Byte.class)
/* 141 */       return (T)Byte.valueOf(value); 
/* 142 */     if (type == Character.class) {
/* 143 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 145 */     return (T)value;
  }




/* 151 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 156 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 161 */     if (uri == null || uri.length() == 0) {
/* 162 */       Object currentObj = this.m_objs.pop();
/* 163 */       String currentTag = (String)this.m_tags.pop();

/* 165 */       if (currentObj instanceof ProcessDomain) {
/* 166 */         ProcessDomain processDomain = (ProcessDomain)currentObj;

/* 168 */         if ("ip".equals(currentTag)) {
/* 169 */           processDomain.addIp(getText());
        }
      }
    }

/* 174 */     this.m_text.setLength(0);
  }


/* 178 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 182 */   protected String getText() { return this.m_text.toString(); }


  private void parseForDetail(Detail parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 186 */     this.m_objs.push(parentObj);
/* 187 */     this.m_tags.push(qName);
  }

  private void parseForMachine(Machine parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 191 */     if ("processDomains".equals(qName)) {
/* 192 */       this.m_objs.push(parentObj);
/* 193 */     } else if ("processDomain".equals(qName)) {
/* 194 */       ProcessDomain processDomain = this.m_maker.buildProcessDomain(attributes);

/* 196 */       this.m_linker.onProcessDomain(parentObj, processDomain);
/* 197 */       this.m_objs.push(processDomain);
/* 198 */     } else if ("message".equals(qName)) {
/* 199 */       Message message = this.m_maker.buildMessage(attributes);

/* 201 */       this.m_linker.onMessage(parentObj, message);
/* 202 */       this.m_objs.push(message);
    } else {
/* 204 */       throw new SAXException(String.format("Element(%s) is not expected under machine!", new Object[] { qName }));
    }

/* 207 */     this.m_tags.push(qName);
  }

  private void parseForMessage(Message parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 211 */     this.m_objs.push(parentObj);
/* 212 */     this.m_tags.push(qName);
  }

  private void parseForProcessDomain(ProcessDomain parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 216 */     if ("ips".equals(qName) || "ip".equals(qName)) {
/* 217 */       this.m_objs.push(parentObj);
/* 218 */     } else if ("detail".equals(qName)) {
/* 219 */       Detail detail = this.m_maker.buildDetail(attributes);

/* 221 */       this.m_linker.onDetail(parentObj, detail);
/* 222 */       this.m_objs.push(detail);
    } else {
/* 224 */       throw new SAXException(String.format("Element(%s) is not expected under processDomain!", new Object[] { qName }));
    }

/* 227 */     this.m_tags.push(qName);
  }

  private void parseForStateReport(StateReport parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 231 */     if ("machine".equals(qName)) {
/* 232 */       Machine machine = this.m_maker.buildMachine(attributes);

/* 234 */       this.m_linker.onMachine(parentObj, machine);
/* 235 */       this.m_objs.push(machine);
    } else {
/* 237 */       throw new SAXException(String.format("Element(%s) is not expected under state-report!", new Object[] { qName }));
    }

/* 240 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 244 */     if ("state-report".equals(qName)) {
/* 245 */       StateReport stateReport = this.m_maker.buildStateReport(attributes);

/* 247 */       this.m_entity = stateReport;
/* 248 */       this.m_objs.push(stateReport);
/* 249 */       this.m_tags.push(qName);
/* 250 */     } else if ("machine".equals(qName)) {
/* 251 */       Machine machine = this.m_maker.buildMachine(attributes);

/* 253 */       this.m_entity = machine;
/* 254 */       this.m_objs.push(machine);
/* 255 */       this.m_tags.push(qName);
/* 256 */     } else if ("processDomain".equals(qName)) {
/* 257 */       ProcessDomain processDomain = this.m_maker.buildProcessDomain(attributes);

/* 259 */       this.m_entity = processDomain;
/* 260 */       this.m_objs.push(processDomain);
/* 261 */       this.m_tags.push(qName);
/* 262 */     } else if ("message".equals(qName)) {
/* 263 */       Message message = this.m_maker.buildMessage(attributes);

/* 265 */       this.m_entity = message;
/* 266 */       this.m_objs.push(message);
/* 267 */       this.m_tags.push(qName);
/* 268 */     } else if ("detail".equals(qName)) {
/* 269 */       Detail detail = this.m_maker.buildDetail(attributes);

/* 271 */       this.m_entity = detail;
/* 272 */       this.m_objs.push(detail);
/* 273 */       this.m_tags.push(qName);
    } else {
/* 275 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 281 */     if (uri == null || uri.length() == 0) {
/* 282 */       if (this.m_objs.isEmpty()) {
/* 283 */         parseRoot(qName, attributes);
      } else {
/* 285 */         Object parent = this.m_objs.peek();
/* 286 */         String tag = (String)this.m_tags.peek();

/* 288 */         if (parent instanceof StateReport) {
/* 289 */           parseForStateReport((StateReport)parent, tag, qName, attributes);
/* 290 */         } else if (parent instanceof Machine) {
/* 291 */           parseForMachine((Machine)parent, tag, qName, attributes);
/* 292 */         } else if (parent instanceof ProcessDomain) {
/* 293 */           parseForProcessDomain((ProcessDomain)parent, tag, qName, attributes);
/* 294 */         } else if (parent instanceof Message) {
/* 295 */           parseForMessage((Message)parent, tag, qName, attributes);
/* 296 */         } else if (parent instanceof Detail) {
/* 297 */           parseForDetail((Detail)parent, tag, qName, attributes);
        } else {
/* 299 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 303 */       this.m_text.setLength(0);
    } else {
/* 305 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }

  protected Date toDate(String str, String format) {
    try {
/* 311 */       return (new SimpleDateFormat(format)).parse(str);
/* 312 */     } catch (ParseException e) {
/* 313 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }

  protected Number toNumber(String str, String format) {
    try {
/* 319 */       return (new DecimalFormat(format)).parse(str);
/* 320 */     } catch (ParseException e) {
/* 321 */       throw new RuntimeException(String.format("Unable to parse number(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/state/model/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */